<?php
namespace Swostar\Server\Http;

use Swostar\Server\Server;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Swoole\Http\Server as SwooleHttpServer;
use Swostar\Message\Http\Request;

/**
 * http server
 * @package Swostar\Server\Http
 */
class HttpServer extends Server
{
    public function createServer()
    {
        $this->swooleServer = new SwooleHttpServer($this->host, $this->port);

    }

    protected function initEvents() {
        $this->setEvent('sub', [
            'request' => 'onRequest',
        ]);
    }



    public function onRequest(SwooleRequest $request, SwooleResponse $response)
    {

        if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
            $response->end();
            return;
        }

        // 请求处理
        $httpRequest = Request::init($request);
        
        // 路由解析
        $result = app('route')->setModule('http')->match($httpRequest->getMethod(), $httpRequest->getUriPath());
   
        $response->end($result);
    }
}
